package actions;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import services.UserService;
import t4p.model.User;

/**
 * Servlet implementation class EditUser
 */
@WebServlet("/editUser")
public class EditUser extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EditUser() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doAction(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doAction(request, response);
	}

	protected void doAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String userFullname = request.getParameter("txtFullname");
		String userDescription = request.getParameter("txtDescription");
		
		HttpSession session = request.getSession(true);
		User loggedUser = (User)session.getAttribute("loggedUser");
		
		if(UserService.getInstace().editAtributtes(loggedUser, userFullname, userDescription)){
			response.sendRedirect(response.encodeRedirectURL("/aaarrgh/pages/editProfile.jsp?success=1"));
		}else{
			response.sendRedirect(response.encodeRedirectURL("/aaarrgh/pages/editProfile.jsp?error=1"));
		};
	}
}
